window.addEventListener('load', function () {
    var goback = document.querySelector('.goback');
    var floor = document.querySelector('.floor');
    var floorTop = floor.offsetTop;
    var gobackTop = floorTop - goback.offsetTop;
    document.addEventListener('scroll', function () {
        // console.log(window.pageYOffset); // 页面被卷曲的头部
        // console.log(floor.offsetTop);
        // console.log(gobackTop);
        if (window.pageYOffset >= floorTop) {
            goback.style.display = "block";
            goback.style.position = "fixed";
        } else {
            goback.style.display = "none";
        }
    })
    goback.addEventListener('click', function () {
        // window.scroll(x, y);  // 当我们点击了返回顶部模块就让窗口滚动到页面的最上方
        // window.scroll(0, 0);
        // 窗口滚动所以对象是window   y轴顶部值为0  
        animation(window, 0);
    })
    function animation(obj, target, callback) {  // 动画函数
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            var step = (target - window.pageYOffset) / 10;
            var step = step > 0 ? Math.ceil(step) : Math.floor(step);
            if (window.pageYOffset == target) {
                clearInterval(obj.timer);
                callback && callback();
            } else {
                // obj.style.left = obj.offsetLeft + step + 'px';
                window.scroll(0, window.pageYOffset + step);   // X永远是0  主要是y轴在滚动
            }
        }, 15);
    }
    // var dropdown = document.querySelector('.dropdown');
    // var nav_dd = document.querySelector('.dropdown').querySelector('.dd');
    // dropdown.addEventListener('mouseover', function () {
    //     nav_dd.style.display = 'block';
    // })
    // dropdown.addEventListener('mouseout', function () {
    //     nav_dd.style.display = 'none';
    // })
    var focus = document.querySelector('.focus');
    var prev = focus.querySelector('.prev');
    var next = focus.querySelector('.next');
    var ul = focus.querySelector('ul');
    var ol = focus.querySelector('.focus_nav');
    var focusWidth = focus.offsetWidth;
    focus.addEventListener('mouseenter', function () {
        prev.style.display = 'block';
        next.style.display = 'block';
        clearInterval(timer);
        timer = null;
    })
    focus.addEventListener('mouseleave', function () {
        prev.style.display = 'none';
        next.style.display = 'none';
        timer = setInterval(function () {
            next.click();
        }, 2000);
    })
    // console.log(ul.children.length);
    for (var i = 0; i < ul.children.length; i++) {
        var li = document.createElement('li');
        li.setAttribute('index', i);
        ol.appendChild(li);
        li.addEventListener('click', function () {
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = '';
            }
            this.className = 'selected';
            // animate(obj,target,callback)
            var index = this.getAttribute('index');
            num = index;
            circle = index; // num = circle = index;
            animate(ul, -index * focusWidth);
        })
    }
    // ol.children[0].style.backgroundColor = '#fff';
    ol.children[0].className = 'selected';
    var first = ul.children[0].cloneNode(true);
    ul.appendChild(first);
    var num = 0;
    var circle = 0;
    var flag = true;
    next.addEventListener('click', function () {
        if (flag) {
            flag = false;
            if (num == ul.children.length - 1) {
                ul.style.left = 0;
                num = 0;
            }
            num++;
            animate(ul, -num * focusWidth, function () {
                flag = true;
            });
            circle++;
            if (circle == ol.children.length) {
                circle = 0;
            }
            circleChange();
        }
    })
    prev.addEventListener('click', function () {
        if (flag) {
            flag = false;
            if (num == 0) {
                num = ul.children.length - 1;
                ul.style.left = -num * focusWidth + 'px';
            }
            num--;
            animate(ul, -num * focusWidth, function () {
                flag = true;
            });
            circle--;
            // if (circle < 0) {
            //     circle = ol.children.length - 1;
            // }
            circle = circle < 0 ? ol.children.length - 1 : circle;
            circleChange();
        }
    })
    function circleChange() {
        for (var i = 0; i < ol.children.length; i++) {
            ol.children[i].className = '';
        }
        ol.children[circle].className = 'selected';
    }
    var timer = setInterval(function () {
        next.click();
    }, 2000)
})
$(function () {
    // 当我们点击了小li  此时不需要执行页面滚动事件里面的li 的背景选择  添加current
    var flag = true;  // 节流阀  互斥锁
    lift();
    function lift() {
        // console.log($(".recom").offset().top);
        if ($(document).scrollTop() >= $(".recom").offset().top) {
            $(".lift").fadeIn();
        } else {
            $(".lift").fadeOut();
        }
    }
    $(window).scroll(function () {
        lift();
        // 页面滚动到某个内容区域，左侧的电梯导航小li相应添加和删除current类名
        if (flag) {
            $(".floor .w").each(function (i, ele) {
                if ($(document).scrollTop() >= $(ele).offset().top) {
                    $(".lift li").eq(i).addClass("current").siblings().removeClass("current");
                }
            })
        }
    })
    $(".lift li").click(function () {
        flag = false;
        // console.log($(this).index());  // 得到当前小li的索引号
        // 每次点击li  就需要计算出页面要去往的位置
        var current = $(".floor .w").eq($(this).index()).offset().top;
        // 页面动画滚动效果
        $("body,html").stop().animate({
            scrollTop: current
        }, function () {
            flag = true;
        });
        // 点击之后让当前的小li添加current类名   兄弟移除current类
        $(this).addClass('current').siblings().removeClass('current')
    })
})